package DAL;

import DatabaseAccess.ConvertDateToString;
import Entities.TblStudent;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class StudentDAL {

    private ResultSet objResult;
    public List<TblStudent> list;

    public StudentDAL() {
    }

    public boolean add(TblStudent student) {
        ConvertDateToString convert = new ConvertDateToString();
        String birthday = convert.convertDateToMMddyyyy(student.getBirthDay());
        int result = 0;        
        String[] params = new String[6];
        params[0] = student.getStudentRollNo();
        params[1] = student.getStudentName();
        params[2] = birthday;
        params[3] = student.getGender()==true?"1":"0";
        params[4] = student.getPhoneNo();
        params[5] = student.getAddress();
        try {
            result = SQLHelper.executeUpdate("ProcInsertStudent", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
        return result == 1 ? true : false;
    }
    public boolean addByBranchId(TblStudent student,int branchId) {
        ConvertDateToString convert = new ConvertDateToString();
        String birthday = convert.convertDateToMMddyyyy(student.getBirthDay());
        int result = 0;        
        String[] params = new String[6];
        params[0] = String.valueOf(branchId);
        params[1] = student.getStudentName();
        params[2] = birthday;
        params[3] = student.getGender()==true?"1":"0";
        params[4] = student.getPhoneNo();
        params[5] = student.getAddress();
        try {
            result = SQLHelper.executeUpdate("procInsertStudentByBranchId", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
        return result == 1 ? true : false;
    }
    public boolean update(TblStudent student) {
        ConvertDateToString convert = new ConvertDateToString();
        String birthday = convert.convertDateToMMddyyyy(student.getBirthDay());
        int result = 0;
        String[] params = new String[6];
        params[0] = student.getStudentRollNo();
        params[1] = student.getStudentName();
        params[2] = birthday;
        params[3] = student.getGender()==true?"1":"0";
        params[4] = student.getPhoneNo();
        params[5] = student.getAddress();
        try {
            result = SQLHelper.executeUpdate("procUpdateStudent", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
        return result == 1 ? true : false;
    }

    public boolean delete(TblStudent student) {
        int result = 0;
        String[] params = new String[1];
        params[0] = student.getStudentRollNo();
        try {
            result = SQLHelper.executeUpdate("procDeleteStudent", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblStudent> getAll() {
        list = new ArrayList<TblStudent>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllStudent", null);
            while (objResult.next()) {
                TblStudent student = new TblStudent();
                student.setStudentRollNo(objResult.getString("StudentRollNo"));
                student.setStudentName(objResult.getString("StudentName"));
                student.setBirthDay(objResult.getDate("BirthDay"));
                student.setGender(objResult.getBoolean("Gender"));
                student.setPhoneNo(objResult.getString("PhoneNo"));
                student.setEmail(objResult.getString("Email"));
                student.setAddress(objResult.getString("Address"));
                student.setJoinDate(objResult.getDate("JoinDate"));
                list.add(student);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblStudent getById(String id) {
        TblStudent student = new TblStudent();
        String[] params = new String[1];
        params[0] = id;
        try {
            objResult = SQLHelper.executeQuery("procGetStudentById", params);
            while (objResult.next()) {
                student.setStudentRollNo(objResult.getString("StudentRollNo"));
                student.setStudentName(objResult.getString("StudentName"));
                student.setBirthDay(objResult.getDate("BirthDay"));
                student.setGender(objResult.getBoolean("Gender"));
                student.setPhoneNo(objResult.getString("PhoneNo"));
                student.setEmail(objResult.getString("Email"));
                student.setAddress(objResult.getString("Address"));
                student.setJoinDate(objResult.getDate("JoinDate"));
            }
            return student;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblStudent> getStudentByClassId(int id) {
        list = new ArrayList<TblStudent>();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetStudentByClassId", params);
            while (objResult.next()) {
                TblStudent student = new TblStudent();
                student.setStudentRollNo(objResult.getString("StudentRollNo"));
                student.setStudentName(objResult.getString("StudentName"));
                student.setBirthDay(objResult.getDate("BirthDay"));
                student.setGender(objResult.getBoolean("Gender"));
                student.setPhoneNo(objResult.getString("PhoneNo"));
                student.setEmail(objResult.getString("Email"));
                student.setAddress(objResult.getString("Address"));
                student.setJoinDate(objResult.getDate("JoinDate"));
                list.add(student);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
